/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seguridad;

import Model.Usuarios;
import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;
import javax.faces.application.NavigationHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpSession;

/**
 *
 * @author jose
 */
public class ValidarSesion implements PhaseListener {

    public void afterPhase(PhaseEvent event) {
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession sesion = (HttpSession) fc.getExternalContext().getSession(true);
        UIViewRoot viewRoot = fc.getViewRoot();
        if (!viewRoot.getViewId().equalsIgnoreCase("/Login.xhtml")) {
            if (sesion.getAttribute("USUARIO") == null) {
                NavigationHandler nh = fc.getApplication().getNavigationHandler();
                nh.handleNavigation(fc, null, "Login");
            }
//            } else {
//                Usuarios u = (Usuarios) sesion.getAttribute("USUARIO");
//                if (u.getTipo().equals("alumno")) {
//                    if (!viewRoot.getViewId().equalsIgnoreCase("/PrincipalAlumnos.xhtml")&&!viewRoot.getViewId().equalsIgnoreCase("/PerfilEgresado.xhtml")) {
//                        NavigationHandler nh = fc.getApplication().getNavigationHandler();
//                        nh.handleNavigation(fc, null, "PrincipalAlumnos");
//                    }
//                }
//            }
        }
    }

    public void beforePhase(PhaseEvent event) {
    }

    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }
}
